The present invention relates to software and more particularly to software for use in compilers and debuggers. Debugging optimized code presents a challenge to both a compiler and debugger alike. A compiler is a computer program that converts code in a source language into code of a target language (often in optimized form), while a debugger is a program used to aid in the design and development of target code.
During compilation, code may be reordered and/or duplicated beyond recognition, and data may be stored at different locations or even be folded and/or removed. A typical compiler optimization is to cache memory operands in registers such that high latency memory accesses can be avoided. For example, a compiler can load a loop-invariant non-volatile memory variable in a loop pre-header, access a processor register in place of the variable, and then store it back to memory upon loop exit.
If the offset of such cached portion of a variable is constant and the mapping to registers is known at compile-time, the compiler can generate one or more live ranges for a debugger to show up-to-date “data which” information fetched from the correct locations. A “live range” is a time and place during execution of a program at which a given variable is located. When a user adds a data watch on a variable X, a debugger must fetch the data from the correct location(s) depending on the current program count (PC) value, otherwise stale data in memory may be presented and confuse the user.
However, generation of live ranges does not work if a variable offset is not constant or one or more registers to which a variable is cached is not known at compile-time. In this case, it is difficult or impossible for a compiler to generate code to access the cached data efficiently and write it back to memory when the offset and/or mapping are unknown. A need thus exists to efficiently look up a non-constant offset and map a variable to a cache.